/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package eac.ucm;
import common.Convertor;
import java.sql.*;
import javax.swing.*;
import eac.model.ValueModel;
import eac.model.ComponentModel;
import eac.view.EACenter;
import java.awt.*;
import java.io.*;
import database.Select;
/**
 *
 * @author Vitamin
 */
public class ShowPersonalInfo {

    private String name;
    private String cid;
    private String group;
    private String college;
    private String path="D:\\TP_BUFFER\\";
    private String id;
    private String tid;
    private ValueModel value;
    private ComponentModel component;
    private JLabel jphoto;
    private JLabel jname;
    private JLabel jgroup;
    private JLabel jcollege;
    private JLabel jcid;
    private Select dbSelect;
    private byte[] buffer=new byte[4096];
    private ResultSet result;
    private File file=null;
    private String sql;
    private JTextArea response;
    private JTextArea tresponse;
    private JLabel tphoto;
    private JLabel tname;
    private JLabel tgroup;
    private JLabel tcollege;
    private JLabel tcid;

    public ShowPersonalInfo()
    {
        value=EACenter.getInstance().getValueModel();
        component=EACenter.getInstance().getComponentModel();
        id=value.getId();
        jphoto=component.getLbphoto();
        jname=component.getLbname();
        jgroup=component.getLbgroup();
        jcollege=component.getLbcollege();
        jcid=component.getLbcid();
        dbSelect=new Select();
        response=component.getResponse();
        tid=value.getTid();
        tresponse=component.getTresponse();
        tphoto=component.getTlbphoto();
        tname=component.getTlbname();
        tgroup=component.getTlbgroup();
        tcollege=component.getTlbcollege();
        tcid=component.getTlbcid();

    }

    public void setStudentValue(){

        try {
            InputStream inputimage=null;
            OutputStream outputimage=null;
            sql="SELECT student.name,student.student_card, student.college, student.groups " +
                    "FROM student WHERE student_id='"+ id +"'";
            result=dbSelect.selectValue(sql);
            result.next();
                name=result.getString("name");
                cid=result.getString("student_card");
                college=result.getString("college");
                group=result.getString("groups");
            jname.setText("Name: "+name);
            jgroup.setText("Group: "+ Convertor.getInstance().studentGroup(Integer.valueOf(group)));
            jcid.setText("Card ID: "+cid);
            jcollege.setText("College: "+ Convertor.getInstance().college(Integer.valueOf(college)));
            response.setText("> 1 object matched");
            sql="SELECT photo FROM roll WHERE id='"+ id +"'";
            result=dbSelect.selectValue(sql);
            result.next();
            path=path+id+".jpg";
            file=new File(path);
            if(!file.exists())
            {
                try{
                 file.createNewFile();
                }
                catch(IOException e)
                {
                e.printStackTrace();
                }
            }
            try{
                outputimage=new FileOutputStream(file);
            }
            catch(FileNotFoundException e)
            {
                e.printStackTrace();
            }

                Blob image=result.getBlob("photo");
                inputimage=image.getBinaryStream();
                int size;
                try{
                    while((size=inputimage.read(buffer))!=-1)
                    {
                     outputimage.write(buffer, 0, size);
                    }
                }
                    catch (IOException e) {
                    e.printStackTrace();
                    }

                outputimage.close();
                inputimage.close();

        Image imgTemp=Toolkit.getDefaultToolkit().createImage(path);
        jphoto.setIcon(new ImageIcon(imgTemp));
        }
        catch(Exception e)
        {
            e.printStackTrace();
        Image imgTemp=Toolkit.getDefaultToolkit().createImage("D:\\TP_PRUNED\\error.jpg");
        jphoto.setIcon(new ImageIcon(imgTemp));
        jname.setText("N/A");
        jgroup.setText("N/A");
        jcid.setText("N/A");
        jcollege.setText("N/A");
        response.setText("> No object matched");
        }
    }

    public void setTeacherValue(){

        try {
            InputStream inputimage=null;
            OutputStream outputimage=null;
            sql="SELECT teacher.name, teacher.teacher_card, teacher.college, teacher.groups FROM teacher WHERE teacher_id='"+ tid +"'";
            result=dbSelect.selectValue(sql);
            result.next();
                name=result.getString("name");
                cid=result.getString("teacher_card");
                college=result.getString("college");
                group=result.getString("groups");
            tname.setText("Name: "+name);
            tgroup.setText("Group: "+ Convertor.getInstance().teacherGroup(Integer.valueOf(group)));
            tcid.setText("Card ID: "+cid);
            tcollege.setText("College: "+ Convertor.getInstance().college(Integer.valueOf(college)));
            tresponse.setText("> 1 object matched");

            sql="SELECT photo FROM roll WHERE id='"+ tid +"'";
            result=dbSelect.selectValue(sql);
            result.next();
            path=path+id+".jpg";
            file=new File(path);
            if(!file.exists())
            {
                try{
                 file.createNewFile();
                }
                catch(IOException e)
                {
                e.printStackTrace();
                }
            }
            try{
                outputimage=new FileOutputStream(file);
            }
            catch(FileNotFoundException e)
            {
                e.printStackTrace();
            }

                Blob image=result.getBlob("photo");
                inputimage=image.getBinaryStream();
                int size;
                try{
                    while((size=inputimage.read(buffer))!=-1)
                    {
                     outputimage.write(buffer, 0, size);
                    }
                }
                    catch (IOException e) {
                    e.printStackTrace();
                    }

                outputimage.close();
                inputimage.close();

        Image imgTemp=Toolkit.getDefaultToolkit().createImage(path);
        tphoto.setIcon(new ImageIcon(imgTemp));
    }
        catch(Exception e)
        {
            e.printStackTrace();
        Image imgTemp=Toolkit.getDefaultToolkit().createImage("D:\\TP_PRUNED\\error.jpg");
        tphoto.setIcon(new ImageIcon(imgTemp));
        tname.setText("N/A");
        tgroup.setText("N/A");
        tcid.setText("N/A");
        tcollege.setText("N/A");
        tresponse.setText("> No object matched");
        }
    }


}
